﻿
Imports std

'protected for parameters_list3
Public Class parameters_list2
    Inherits iScript
    Implements iParametersBetweenContextWithMapping
    Public Const separator As Char = character.comma
    Protected ps As vector(Of value) = Nothing

    Protected Friend Shared Function _isMeaningfulMark(ByVal c As Char) As Boolean
        Return isseparator(c)
    End Function

    Protected Sub New(ByVal ips As vector(Of value))
        ps = ips
    End Sub

    Public Function at(ByVal i As Int64, ByRef o As bare_variable, ByVal ctx As context) As Boolean
        Return ps(i).invoke(ctx, o)
    End Function

    Private Function at(ByVal i As Int64, ByRef o As bare_variable, ByVal obj As Object) As Boolean
        Return at(i, o, convertor.cast(Of context)(obj))
    End Function

    Public Function size() As Int64
        Return ps.size()
    End Function

    Private Function size(ByVal obj As Object) As Int64
        Return size()
    End Function

    Private Function _invoke(ByVal dest As context, ByVal src As context, ByVal m As iParametersMapping) As Boolean
        Return invokemapping(dest, AddressOf size, AddressOf at, m, src)
    End Function

    Protected Overrides Function invokeImpl(Of T)(ByVal dest As context, ByVal src As context, _
                                                  ByRef obj As T) As Boolean
        Return _invoke(dest, src, convertor.cast(Of iParametersMapping)(obj))
    End Function

    Public Function invoke(ByVal dest As context, ByVal src As context, ByVal m As iParametersMapping) As Boolean _
                          Implements iInvokeableBetweenContextWithMapping.invoke
        Return invokebase(dest, src, m)
    End Function

    Private Shared Function isseparator(ByVal c As Char) As Boolean
        Return c = separator
    End Function

    Protected Shared Function parseImpl(ByVal input As String, ByRef index As Int64, _
                                        ByRef vs As vector(Of value)) As Boolean
        If vs Is Nothing Then
            vs = New vector(Of value)()
        End If
        Dim v As value = Nothing
        If value.parse(input, index, v) Then
            vs.push_back(v)
            While token(input, index, Nothing, AddressOf isseparator, AddressOf [false])
                If value.parse(input, index, v) Then
                    vs.push_back(v)
                Else
                    Return False
                End If
            End While
        End If

        Return True
    End Function

    Protected Shared Function parseImpl(ByVal input As String, ByRef index As Int64, _
                                        ByRef output As parameters_list2) As Boolean
        Dim vs As vector(Of value) = Nothing
        If parseImpl(input, index, vs) Then
            output = New parameters_list2(vs)
            Return True
        Else
            Return False
        End If
    End Function

    Public Shared Function parse(ByVal input As String, ByRef index As Int64, _
                                 ByRef output As parameters_list2) As Boolean
        Return parsebase(input, index, output, AddressOf parseImpl)
    End Function
End Class
